import Vue from 'vue';
//当前ajax请求队列
Vue.prototype.$httpRequestList = [];
import NProgress from 'nprogress'
Vue.prototype.$NProgress=NProgress
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';

import 'nprogress/nprogress.css'
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
import * as filter from './components/common/filter';
import upload from './components/common/j-upload.vue';
Vue.component('upload', upload)
Object.keys(filter).forEach(key => {
  Vue.filter(key, filter[key])
})

Vue.config.productionTip = false;

Vue.use(ElementUI, {
    size: 'small'
});
//配置路由权限,某个路由下配置auth项，如果有auth权限验证，resourceCodes中
//有一项满足要求有权访问，否则该用户无权访问
let resourceCodes=['home','dashboard']

//配置beforeRouter不出现进度条路由名单
let pageNoProgress=['/dashboard']
const hasPermission = (route, resourceCodes = []) => {
  if (route.meta && route.meta.auth && Array.isArray(route.meta.auth)) {
   
    return route.meta.auth.some(code => resourceCodes.includes(code))
  }
  return true
}
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem('token')
  if(Vue.prototype.$httpRequestList.length>0){ 
      Vue.prototype.$httpRequestList.forEach(item=>{ 
        item('interrupt');//路由跳转中断之前全部请求
      })
  }
    
  if (pageNoProgress.indexOf(to.path) >= 0) {
  
  } else {
    NProgress.inc();
  }
  document.title = `${to.meta.title} | 江西省水土保持综合管理平台`;
  //配置不需要登录信息页面名单
  const whiteList = ['/login']
  if (token) {
    console.log(to,from)
    if (to.path=='/login'||to.path=='/403') {
      next()
    } else {
      if (hasPermission(to, resourceCodes)) {  
        next()
      } else {
        next({
          path:'/403',
          replace:true
        })
        
      }
    }
  } else {
    if (whiteList.indexOf(to.path) >= 0) {
      next()
    } else {
      NProgress.done()
      next('/login') 
    }
  }
})
router.afterEach((to) => {
  if (pageNoProgress.indexOf(to.path) >= 0) {
   
  } else {
    NProgress.done()
  }
  
})
new Vue({
    router,
    render: h => h(App)
}).$mount('#app');
